package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;

    @Override
    public Object getBusinessData() {
        //获得今天的日期
        LocalDateTime begin = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Integer newUsers = userMapper.getNewUserStatistics(begin, end);
        Integer validOrderCount = orderMapper.getCount(Orders.COMPLETED, begin, end);
        Integer allOrderCount = orderMapper.getCount(null, begin, end);
        double orderCompletionRate = validOrderCount * 1.0 / allOrderCount;
        double turnover = orderMapper.getTurnoverStatistics(begin, end);
        double unitSales = turnover * 1.0 / validOrderCount;
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitSales)
                .build();
    }

    @Override
    public Object getOverviewSetmeals() {
        int sold =setmealMapper.getByStatus(StatusConstant.ENABLE);
        int discontinued = setmealMapper.getByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public Object getOverviewDishes() {
        int sold = dishMapper.getByStatus(StatusConstant.ENABLE);
        int discontinued = dishMapper.getByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public Object getOverviewOrders() {
        int allOrders=orderMapper.getCount(null,null,null);
        int cancelledOrders=orderMapper.getCount(Orders.CANCELLED,null,null);
        int completedOrders=orderMapper.getCount(Orders.COMPLETED,null,null);
        int deliveredOrders=orderMapper.getCount(Orders.CONFIRMED,null,null);
        int waitingOrders=orderMapper.getCount(Orders.TO_BE_CONFIRMED,null,null);
        return OrderOverViewVO
                .builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
